#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
文科生学编程 - Flask后端
为前端提供API接口支持
"""

from flask import Flask, request, jsonify, render_template
import json
import os
from datetime import datetime

app = Flask(__name__)

# 存储用户进度
USER_DATA_FILE = 'user_progress.json'

def load_user_data():
    """加载用户数据"""
    if os.path.exists(USER_DATA_FILE):
        with open(USER_DATA_FILE, 'r', encoding='utf-8') as f:
            return json.load(f)
    return {}

def save_user_data(data):
    """保存用户数据"""
    with open(USER_DATA_FILE, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

@app.route('/')
def index():
    """主页"""
    return app.send_static_file('index.html')

@app.route('/api/execute', methods=['POST'])
def execute_code():
    """执行Python代码"""
    try:
        data = request.json
        code = data.get('code', '')
        
        # 创建一个安全的执行环境
        safe_globals = {
            '__builtins__': {
               